package wechat.rcv.login.service;

import com.shrct.businessprocess.organization.RCEmployee;
import com.shrct.businessprocess.organization.RCEmployeeUser;
import com.uniquesoft.gwt.server.service.common.HttpRequestThreadLocal;
import edu.fudan.langlab.domain.organization.Party;
import edu.fudan.langlab.domain.security.ISystemUserService;
import edu.fudan.langlab.domain.security.User;
import edu.fudan.mylang.pf.BaseService;
import edu.fudan.mylang.pf.IObjectFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("com.shrcl.engineerprocess.web.server.service.login.SystemUserService")
public class SystemUserServiceImpl extends BaseService implements ISystemUserService {
  @Autowired
  private IObjectFactory objectFactory;
  
  public User getCurrentUser() {
	  try{
    User _xblockexpression = null;
    {
      HttpServletRequest req = HttpRequestThreadLocal.get();
      HttpSession _session = req.getSession();
      Object _attribute = _session.getAttribute("__userid__");
      Long userid = ((Long) _attribute);
      if(userid==null )_xblockexpression = null;
      else{
    	  User _get = this.objectFactory.<User>get(User.class, userid);
    	  _xblockexpression = (_get);
      }
    }
    return _xblockexpression;
	  }catch(Exception e){
		  return null;
	  }
  }
  
  public void setCurrentUser(final User user) {
    HttpServletRequest req = HttpRequestThreadLocal.get();
    HttpSession _session = req.getSession();
    Long _id = user.getId();
    _session.setAttribute("__userid__", _id);
  }
  
  public Party getCurrentUserAsParty() {
    User _currentUser = this.getCurrentUser();
    RCEmployee _employee = ((RCEmployeeUser) _currentUser).getEmployee();
    return ((Party) _employee);
  }

@Override
public void logout() {
    HttpServletRequest req = HttpRequestThreadLocal.get();
    HttpSession _session = req.getSession();
    _session.removeAttribute("__userid__");
	
}
}
